require 'mu_api'

# Sample for your basic daily regression run of an existing analysis
#
# Create the API object
api = Mu_Api.new

# Set host, username, password for the mu4000:
# api.host = "10.10.5.71"
# api.username = "admin"
# api.password = "secret"

# Alternatively,
# EXPORT MU_IP=10.10.5.71; EXPORT MU_USER=admin; EXPORT MU_PASS=secret


# Post and run a new template. In this mode, the new uuid is remembered 
# by the api instance, so you don't need to included it for api.run:
#
# analysis = File.read("sample.xml")
# api.post(analysis)
# api.run

# or Run an existing template that you've already loaded with a new name
z = Time.now.strftime("%b-%d %H:%M")
newname = "regression #{z}"

# Need to get the uuid from the existing template
uuid = "6d32f9f5-1ef3-4cae-96c3-223652241218"
status = api.run(uuid, newname); puts "Run Status: #{status}"

# wait for analysis to start, then poll until it's done
sleep 30
100.times do |poll|
  status = api.status
  break unless status =~ /(RUNNING|QUEUED)/
  sleep 15
end

# save the results archive
# kick off the archive job
archiving = api.archive; puts "Archiving: #{archiving}"
sleep 5
# poll until the job is complete
20.times do
  status = api.archive("status"); puts "Status: #{status}"
  break if status == "Finished"
  sleep 5
end
# download the archive zip file, which is named by the job number
filename = api.archive("get"); puts "Filename: #{filename}"
